package com.j1ay.forum.controller;

import com.j1ay.forum.model.dto.params.PageParams;
import com.j1ay.forum.model.dto.params.RegisterParams;
import com.j1ay.forum.model.dto.params.UpdateUserParams;
import com.j1ay.forum.model.pojo.po.User;
import com.j1ay.forum.model.pojo.vo.CodeVo;
import com.j1ay.forum.service.UserService;
import com.j1ay.forum.model.pojo.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author J1ay
 * @Date 2021/12/16 21:00
 * @Description 用户
 */

@RestController
@RequestMapping("api/user")
@Api(value="User Controller",tags={"用户类接口"})
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取验证码，uuid绑定")
    @GetMapping("code")
    public CodeVo getCode() {
        return userService.getCode();
    }

    @ApiOperation("用户注册, 管理端注册记得加上RoleId，用户不用")
    @PostMapping("register")
    public Result Register(@RequestBody RegisterParams registerParams) {
        return userService.register(registerParams);
    }

    @ApiOperation("用户列表根据角色查询,-1为全部（权限)")
    @PreAuthorize("hasAnyAuthority('is:admin', 'user:list')")
    @PostMapping("list/{roleId}")
    public Result getUserList(@RequestBody PageParams pageParams,  @PathVariable Long roleId) {
        return userService.getUserList(pageParams, roleId);
    }

    @ApiOperation("用户修改个人信息，权限：自己")
    @PutMapping("update/self")
    public Result updateSelf(@RequestBody UpdateUserParams updateUserParams) {
        return userService.updateSelf(updateUserParams);
    }

    @ApiOperation("获取用户个人信息")
    @PostMapping("self/{id}")
    public Result getSelf(@PathVariable("id") Long id) {
        return userService.getSelf(id);
    }

    @ApiOperation("修改用户信息")
    @PutMapping("update/info")
    @PreAuthorize("hasAnyAuthority('is:admin', 'user:update')")
    public Result updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @ApiOperation("删除用户信息（管理员)")
    @PreAuthorize("hasAnyAuthority('is:admin', 'user:update')")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return userService.delete(id);
    }

    @ApiOperation("获取用户个人浏览量点赞数等数据")
    @GetMapping("count/{username}")
    public Result getData(@PathVariable("username") String username) {
        return userService.getData(username);
    }

    @ApiOperation("禁用或解禁账号")
    @PutMapping("lock/{userId}")
    public Result lockUser(@PathVariable("userId") Long userId) {
        return userService.lockUser(userId);
    }
}
